package br.com.jpoker.common.ui.action;

import java.util.Timer;
import java.util.TimerTask;

import br.com.jpoker.model.Deck;
import br.com.jpoker.model.SitPlace;
import br.com.jpoker.model.Table;

/**
 *
 */
public class DrawCardsAction{
	
	private Timer timer;
	private Table table;
	int playerSitIndexes[];
	int stopIndex = 0;
	boolean secondCard = false;
	
	public DrawCardsAction(Table table) {
		this.table = table;
		playerSitIndexes = new int[table.getPlayers().size()];
		
		int i = 0;
		for(SitPlace sp : table.getSitPlaces()){
			if(sp.getPlayer() != null){
				playerSitIndexes[i++] = sp.getSitNumber()-1;
			}
		}
		
		timer = new Timer();
		timer.scheduleAtFixedRate(new DrawCardsTask(), 0, 1 * 200); 
	}

	class DrawCardsTask extends TimerTask {
		public void run() {
			if(stopIndex == playerSitIndexes.length){
				if(secondCard){
					timer.cancel();
					return;
				}
				stopIndex = 0;
				secondCard = true;
			}
			if(secondCard){
				table.getPlaceHolderCards()[table.getSitPlaces().length + playerSitIndexes[stopIndex]].setIcon(Deck.getInstance().drawCard().getImage());
			}else{
				table.getPlaceHolderCards()[playerSitIndexes[stopIndex]].setIcon(Deck.getInstance().drawCard().getImage());
			}

			stopIndex++;
			//table.getDealer().dealCard(table.getPlayers().get(index));
		}
	}

}